//
//  ThreeVCManager.m
//  ThreeVCManager
//
//  Created by lanou3g on 16/3/10.
//  Copyright © 2016年 Wicky. All rights reserved.
//

/*
            ThreeVCManager
        通过三个子控制器模拟左右滑动翻页效果。根据需要修改setAVC方法来设置每个控制器。根据需要也可修改左右滑动手势实现刷新及跳转功能（此版本为非循环版本）
 */
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#import "ThreeVCManager.h"

@interface ThreeVCManager ()
@property(strong,nonatomic)UIViewController * LeftVC;
@property(strong,nonatomic)UIViewController * MidVC;
@property(strong,nonatomic)UIViewController * RightVC;
@property(assign,nonatomic)NSInteger currentPage;
@property(assign,nonatomic)NSInteger countOfPage;//根据情况自定义页面总数
@end

@implementation ThreeVCManager

- (void)viewDidLoad {
    [super viewDidLoad];
    [self valueInit];
    [self addGesture];
    [self setThreeVC];
    [self addThreeVC];
}
-(void)valueInit
{
    self.currentPage = 0;
    self.countOfPage = 5;
}
-(void)addGesture
{
    UISwipeGestureRecognizer * swipLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipLeftAction)];
    swipLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipLeft];
    UISwipeGestureRecognizer * swipRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipRightAction)];
    swipRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipRight];
}
-(void)swipLeftAction
{
    
    if (self.currentPage < self.countOfPage - 1) {
        [UIView animateWithDuration:0.4 animations:^{
            self.MidVC.view.frame = CGRectMake(-kScreenW, 0, kScreenW, kScreenH);
            self.RightVC.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
            [self.LeftVC removeFromParentViewController];
            [self.LeftVC.view removeFromSuperview];
            self.LeftVC = self.MidVC;
            self.MidVC = self.RightVC;
            if (self.currentPage < self.countOfPage - 2) {
                self.RightVC = [self setAVC:self.currentPage + 2];
            }
            else
            {
                self.RightVC = [UIViewController new];
            }
        }];
        [self addThreeVC];
        self.currentPage ++;
    }
}
-(void)swipRightAction
{
    if (self.currentPage > 0) {
        [UIView animateWithDuration:0.4 animations:^{
            self.MidVC.view.frame = CGRectMake(kScreenW, 0, kScreenW, kScreenH);
            self.LeftVC.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
            [self.RightVC removeFromParentViewController];
            [self.RightVC.view removeFromSuperview];
            self.RightVC = self.MidVC;
            self.MidVC = self.LeftVC;
            if (self.currentPage > 1) {
                self.LeftVC = [self setAVC:self.currentPage - 2];
            }
            else
            {
                self.LeftVC = [UIViewController new];
            }
        }];
        [self addThreeVC];
        self.currentPage --;
    }
}
-(UIViewController *)setAVC:(NSInteger)number//可根据情况添加函数参数来加载不同界面，例如可传入url加载数据等等
{
    UIViewController * new = [UIViewController new];
    new.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    return new;
}
-(void)setThreeVC
{
    if (self.currentPage == 0) {
        self.LeftVC = [UIViewController new];
    }
    else
    {
        self.LeftVC = [self setAVC:self.currentPage - 1];
    }
    self.MidVC = [self setAVC:self.currentPage];
    if (self.currentPage == self.countOfPage - 1) {
        self.RightVC = [UIViewController new];
    }
    else
    {
        self.RightVC = [self setAVC:self.currentPage + 1];
    }
}
-(void)addThreeVC
{
    int i = -1;
    NSArray * arrTemp = @[self.LeftVC,self.MidVC,self.RightVC];
    for (UIViewController * VC in arrTemp)
    {
        [VC removeFromParentViewController];
        [VC.view removeFromSuperview];
        VC.view.frame = CGRectMake(kScreenW * i, 0, kScreenW, kScreenH);
        [self addChildViewController:VC];
        [self.view addSubview:VC.view];
        i++;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
